package com.jed.xxh.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.jed.xxh.R;
import com.jed.xxh.activity.NewsActivity;
import com.jed.xxh.adapter.NewsAdapter;
import com.jed.xxh.entity.NewsData;
import com.jed.xxh.utils.L;
import com.jed.xxh.utils.StaticClass;
import com.jed.xxh.utils.T;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 项目名：stewardAPP<br/>
 * 包名：  com.jed.xxh.fragment<br/>
 * 文件名：WechatFragment<br/>
 * 作者：徐晓辉<br/>
 * 创建日期：2018/11/24 14:04<br/>
 * 描述：头条新闻<br/>
 */
public class NewsFragment extends Fragment implements AdapterView.OnItemClickListener {


    private ListView mListView;
    private NewsAdapter adapter;
    private List<NewsData> mList = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mListView = view.findViewById(R.id.mListView);
        mListView.setOnItemClickListener(NewsFragment.this);
        getNews();
    }

    /**
     * 获取头条新闻
     */
    private void getNews() {
        HttpParams params = new HttpParams();
        params.put("key", StaticClass.JH_NEWS_KEY);
        params.put("type", "top");
        RxVolley.get(StaticClass.JH_NEWS_URL, params, new HttpCallback() {
            @Override
            public void onSuccess(String t) {
                L.i("获取到的头条新闻的数据：" + t);
                parsingJson(t);
            }
        });
    }

    /**
     * 解析数据
     */
    private void parsingJson(String t) {
        try {
            JSONObject jsonObject = new JSONObject(t);
            String error_code = jsonObject.getString("error_code");
            if ("0".equals(error_code)) {
                JSONObject result = jsonObject.getJSONObject("result");
                JSONArray list = result.getJSONArray("data");
                Gson gson = new Gson();
                NewsData[] newsData = gson.fromJson(list.toString(), NewsData[].class);
                mList = Arrays.asList(newsData);
                adapter = new NewsAdapter(mList);
                mListView.setAdapter(adapter);
            } else {
                T.showShortToast("加载失败...");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    /**
     * listView的点击事件
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        NewsData newsData = mList.get(position);
        Intent intent = new Intent(getActivity(), NewsActivity.class);
        intent.putExtra("title", newsData.getTitle());
        intent.putExtra("url", newsData.getUrl());
        startActivity(intent);
    }
}
